import { getOrderDetail, editOrder } from "../../api/index";
import { store } from "../../../store/index";

Page({
  data: {
    id: "",
    type: '',
    pageData: {},
  },
  onLoad(options) {
    console.log(options, 'options');
    
    this.setData({
      id: options.id,
      type: options.type,
    });
  },
  onShow() {
    this.getOrderDetail();
  },
  getOrderDetail() {
    getOrderDetail({ id: this.data.id }).then((res) => {
      console.log(res, "res");
      this.setData({
        pageData: {
          ...res.result,
          titleType: res.result.type.split("")[0],
        },
      });
    });
  },
  telService() {
    wx.makePhoneCall({
      phoneNumber: this.data.pageData.userAddr.phone
    })
  },
  confirmOrder() {
    console.log(this.data.id, 'id');
    wx.navigateTo({
      url: `/pages/order/receiverConfirm/index?id=${this.data.id}`,
    });
  },
  confirmData() {
    wx.showModal({
      title: "提示",
      content: "确认完成后金额将转入到接单员账户",
      confirmColor: "#409eff",
      confirmText: "确定",
      cancelText: "取消",
      success: (res) => {
        if (res.confirm) {
        editOrder({
          id: this.data.id,
          status: "发单员确认完成",
        }).then((res) => {
          wx.showToast({
            title: "确认完成",
            icon: "success",
          });
          wx.navigateBack({
            delta: 1,
          });
        });
        }
      }
    });
  },
  helpOrder() {
    if (store.userInfo.applyStatus !== "通过") {
      wx.showModal({
        title: "提示",
        content: "您还没有成为接单员，是否前往申请。",
        confirmColor: "#409eff",
        confirmText: "立即前往",
        cancelText: "取消",
        success: (res) => {
          if (res.confirm) {
            // 用户点击了确定
            console.log("用户点击确定");
            wx.navigateTo({
              url: `/pages/me_content/aplay`,
            });
          }
        },
      });
      return;
    }


    wx.showModal({
      title: "提示",
      content: "确定要接此单吗",
      confirmColor: "#409eff",
      confirmText: "立即接单",
      cancelText: "取消",
      success: (res) => {
        if (res.confirm) {
          editOrder({
            id: this.data.id,
            status: "进行中",
            receiverId: store.userInfo.id,
          }).then((res) => {
            console.log(res, "res");
            wx.showToast({
              title: "接单成功",
              icon: "success",
            });
            wx.navigateBack({
              delta: 1,
            });
          });
        }
      },
    });
  },

  rejectOrder() {
    wx.showModal({
      title: "提示",
      content: "确定要驳回此单吗,驳回后订单会被其他接单员接单",
      confirmColor: "#409eff",
      confirmText: "确定",
      cancelText: "取消",
      success: (res) => {
        if (res.confirm) {
          editOrder({
            id: this.data.id,
            status: "驳回",
          }).then((res) => {
            wx.showToast({
              title: "驳回成功",
              icon: "success",
            });
            wx.navigateBack({
              delta: 1,
            });
          });
        }
      },
    });
  },
});
